unit KmBookData;

interface

uses
  FMX.Graphics;

type

  TBookData = class
  private
    function GetISBNNumeric: string;
    function GetObalkaBlobAsArray: TArray<Byte>;
{$IF not Defined(iOS) and not Defined(ANDROID)}
    function GetObalkaBlobAsString: RawByteString;
{$ENDIF}
  protected
    FId: integer;
    FISBN: string;
    FTitul: string;
    FPodtitul: string;
    FAutori: string;
    FZanr: string;
    FVydavatel: string;
    FPocetStran: integer;
    FDatumVydani: TDateTime;
    FDatumVydaniText: string;
    FAnotace: string;
    FObalka: TBitmap;
  public
    constructor Create;
    destructor Destroy;
    procedure Join(ABookData: TBookData);

    property Id: integer read FId write FId;
    property ISBN: string read FISBN write FISBN;
    property ISBNNumeric: string read GetISBNNumeric;
    property Titul: string read FTitul write FTitul;
    property Podtitul: string read FPodtitul write FPodtitul;
    property Autori: string read FAutori write FAutori;
    property Zanr: string read FZanr write FZanr;
    property Vydavatel: string read FVydavatel write FVydavatel;
    property PocetStran: integer read FPocetStran write FPocetStran;
    property DatumVydani: TDateTime read FDatumVydani write FDatumVydani;
    property DatumVydaniText: string read FDatumVydaniText write FDatumVydaniText;
    property Anotace: string read FAnotace write FAnotace;
    property Obalka: TBitmap read FObalka write FObalka;
    property ObalkaBlobAsArray: TArray<Byte> read GetObalkaBlobAsArray;
{$IF not Defined(iOS) and not Defined(ANDROID)}
    property ObalkaBlobAsString: RawByteString read GetObalkaBlobAsString;
{$ENDIF}
  end;

implementation

uses
  System.SysUtils, System.Classes;

{ TBook }

constructor TBookData.Create;
begin
  inherited;
  FObalka := TBitmap.Create;
end;

destructor TBookData.Destroy;
begin
  FObalka.Free;
end;

function TBookData.GetISBNNumeric: string;
var
  c: char;
begin
  Result := '';
  for c in FISBN do
    if CharInSet(c,['0'..'9']) then
      Result := Result + c;
end;

function TBookData.GetObalkaBlobAsArray: TArray<Byte>;
var
  MS: TMemoryStream;
begin
  MS := TMemoryStream.Create;
  FObalka.SaveToStream(MS);
  SetLength(Result, MS.Size);
  MS.Position := 0;
  MS.Read(Result, MS.Size);
  MS.Free;
end;

{$IF not Defined(iOS) and not Defined(ANDROID)}
function TBookData.GetObalkaBlobAsString: RawByteString;
var
  AB: TArray<Byte>;
begin
  AB := GetObalkaBlobAsArray;
  SetString(Result, PAnsiChar(pointer(AB)), length(AB));
end;
{$ENDIF}


procedure TBookData.Join(ABookData: TBookData);

  procedure JoinItem(var Item, NewItem: string); overload;
  begin
    if Trim(Item) = '' then //length(NewItem) > length(Item) then
      Item := NewItem;
  end;

  procedure JoinItem(var Item, NewItem: integer); overload;
  begin
    if NewItem > Item then
      Item := NewItem;
  end;

  procedure JoinItem(var Item, NewItem: TDateTime); overload;
  begin
    if NewItem > Item then
      Item := NewItem;
  end;

begin
  JoinItem(FISBN, ABookData.FISBN);
  JoinItem(FTitul, ABookData.FTitul);
  JoinItem(FPodtitul, ABookData.FPodtitul);
  JoinItem(FAutori, ABookData.FAutori);
  JoinItem(FZanr, ABookData.FZanr);
  JoinItem(FVydavatel, ABookData.FVydavatel);
  JoinItem(FPocetStran, ABookData.FPocetStran);
  JoinItem(FDatumVydani, ABookData.FDatumVydani);
  JoinItem(FDatumVydaniText, ABookData.FDatumVydaniText);
  if Obalka.Width = 0 then
    FObalka.Assign(ABookData.FObalka);
  JoinItem(FAnotace, ABookData.FAnotace);
end;

end.

